﻿using System;
using System.IO;
using System.Web.UI;
using System.Web;

namespace Hxsoft.Web.UI.Adapters
{
	public class FormRewriterControlAdapter : System.Web.UI.Adapters.ControlAdapter
	{
		protected override void Render(System.Web.UI.HtmlTextWriter writer)
		{
			base.Render(new RewriteFormHtmlTextWriter(writer));
		}
	}

	public class RewriteFormHtmlTextWriter : System.Web.UI.HtmlTextWriter
	{
		public RewriteFormHtmlTextWriter(HtmlTextWriter writer)
			: base(writer)
		{
		}

		public RewriteFormHtmlTextWriter(TextWriter writer)
			: base(writer)
		{
		}

		public override void WriteAttribute(string name, string value, bool fEncode)
		{
			if (name == "action")
			{
				HttpContext context = HttpContext.Current;
				if (context.Items["ActionAlreadyWritten"] == null)
				{
					value = "";
					context.Items["ActionAlreadyWritten"] = true;
				}
			}

			base.WriteAttribute(name, value, fEncode);
		}
	}
}